﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinLOAtest.Data;
using WinLOAtest.Forms;
using WinLOAtest.Test;

namespace WinLOAtest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeDatabase();
        }

        /// <summary>
        /// 初始化数据库
        /// </summary>
        private void InitializeDatabase()
        {
            try
            {
                DatabaseHelper.InitializeDatabase();
                MessageBox.Show("数据库初始化成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"数据库初始化失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 生成一个20位长度的字符串，在指定的多个位置（1-based）上设置为 '1'，其余为 '0'
        /// </summary>
        /// <param name="positions">要设置为 '1' 的位置集合（1-based，例如 {1, 3, 5}）</param>
        /// <param name="length">字符串总长度，默认为 20</param>
        /// <returns>指定长度的二进制风格字符串</returns>
        public static string GenerateBinaryString(IEnumerable<int> positions, int length = 20)
        {
            // 输入验证
            if (positions == null)
                throw new ArgumentNullException(nameof(positions));

            var posList = positions.Distinct().ToList(); // 去重

            foreach (int pos in posList)
            {
                if (pos < 1 || pos > length)
                    throw new ArgumentOutOfRangeException(nameof(positions), $"位置必须在 1 到 {length} 之间，但提供了 {pos}。");
            }

            // 初始化为 length 个 '0'
            char[] chars = new char[length];
            for (int i = 0; i < length; i++)
            {
                chars[i] = '0';
            }

            // 将指定位置设为 '1'（转换为 0-based 索引）
            foreach (int pos in posList)
            {
                chars[pos - 1] = '1';
            }

            return new string(chars);
        }

        /// <summary>
        /// 账号管理菜单点击事件
        /// </summary>
        private void menuAccountManagement_Click(object sender, EventArgs e)
        {
            var accountForm = new AccountManagementForm();
            accountForm.ShowDialog();
        }

        /// <summary>
        /// 功能配置菜单点击事件
        /// </summary>
        private void menuFeatureConfig_Click(object sender, EventArgs e)
        {
            var featureForm = new FeatureConfigForm();
            featureForm.ShowDialog();
        }

        /// <summary>
        /// 退出菜单点击事件
        /// </summary>
        private void menuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// 测试菜单点击事件
        /// </summary>
        private void menuTest_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("是否运行GenerateBinaryString方法测试？", "测试确认", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            
            if (result == DialogResult.Yes)
            {
                // 创建测试窗口
                var testForm = new Form
                {
                    Text = "GenerateBinaryString 方法测试",
                    Size = new Size(600, 400),
                    StartPosition = FormStartPosition.CenterParent
                };

                var textBox = new TextBox
                {
                    Multiline = true,
                    ScrollBars = ScrollBars.Vertical,
                    Dock = DockStyle.Fill,
                    Font = new Font("Consolas", 9),
                    ReadOnly = true
                };

                testForm.Controls.Add(textBox);

                // 重定向控制台输出到文本框
                var stringWriter = new System.IO.StringWriter();
                var originalOut = Console.Out;
                Console.SetOut(stringWriter);

                try
                {
                    TestGenerateBinaryString.RunTests();
                    TestGenerateBinaryString.RunExceptionTests();
                }
                finally
                {
                    Console.SetOut(originalOut);
                }

                textBox.Text = stringWriter.ToString();
                testForm.ShowDialog();
            }
        }
    }
}
